package com.renwei.yunlong.activity;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.NotificationManagerCompat;
import android.text.TextUtils;

import com.renwei.yunlong.R;
import com.renwei.yunlong.YunLongApplication;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.utils.DialogUtils;
import com.renwei.yunlong.utils.LogUtil;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yanzhenjie.permission.PermissionListener;
import com.yanzhenjie.permission.PermissionUtil;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;
import com.zyf.fwms.commonlibrary.utils.SharedPreUtil;

import java.util.List;


/**
 * 启动页
 * Created by Administrator on 2017/8/22.
 */

public class WelcomeActivity extends BaseActivity implements RationaleListener, PermissionListener {

    private static final int PERMISSION_IM_LOCATION = 1001;
    private final long SPLASH_LENGTH = 500;
    Handler handler = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        PermissionUtil.checkMorePermission(this, PERMISSION_IM_LOCATION, this, this, Permission.STORAGE, Permission.LOCATION);
        NotificationManagerCompat manager = NotificationManagerCompat.from(YunLongApplication.getInstance());
        boolean isOpened = manager.areNotificationsEnabled();
        LogUtil.e("通知权限是否开启 = " + isOpened);
    }

    @Override
    public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
        AndPermission.rationaleDialog(this, rationale).show();
    }

    @Override
    public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
        switch (requestCode) {
            case PERMISSION_IM_LOCATION:
                handler.postDelayed(new Runnable() {  //使用handler的postDelayed实现延时跳转
                    @Override
                    public void run() {
                        //判断是不是第一次进入、如果是进入引导页面、不是进入首页
                        // 判断之前有没有显示过新手引导
                        boolean userGuide = SharedPreUtil.getBoolean(WelcomeActivity.this, "isGuide", false);
                        if (TextUtils.isEmpty(companyType)) {
                            if (!userGuide) {
                                initDefaultInfo();
                                // 跳转到新手引导页
                                startActivity(new Intent(WelcomeActivity.this, GuideActivity.class));
                                finish();
                            } else {
                                if (TextUtils.isEmpty(YunLongApplication.BASE_URL)) {
                                    initApi();
                                }
                                LoginActivity.openLoginActivity(WelcomeActivity.this);
                                finish();
                            }
                        } else {
                            if (TextUtils.isEmpty(YunLongApplication.BASE_URL)) {
                                initApi();
                            }
                            startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
                            finish();
                        }
                    }
                }, SPLASH_LENGTH);//500毫秒后跳转
                break;
            default:
                break;
        }
    }

    @Override
    public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
        switch (requestCode) {
            case PERMISSION_IM_LOCATION:
                if (AndPermission.hasAlwaysDeniedPermission(this, deniedPermissions)) {
                    // 第一种：用默认的提示语。
                    DialogUtils.showDialog(mContext, "应用权限申请失败", false, new DialogUtils.OnDialogClickListener() {
                        @Override
                        public void onCancel(Dialog dialog) {
                            dialog.dismiss();
                            finish();
                        }

                        @Override
                        public void onConfirm(Dialog dialog) {
                            Uri packageURI = Uri.parse("package:" + getPackageName());
                            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                            startActivity(intent);
                            dialog.dismiss();
                        }
                    });
                }
                break;
            default:
                break;
        }
    }
}
